package main

import (
	"gitee.com/llqhz/go-micro-user/user/domain/repository"
	user_service "gitee.com/llqhz/go-micro-user/user/domain/service"
	"gitee.com/llqhz/go-micro-user/user/handler"
	pb "gitee.com/llqhz/go-micro-user/user/proto/user"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"

	"github.com/micro/micro/v3/service"
	"github.com/micro/micro/v3/service/logger"
)

func main() {
	// Create service
	srv := service.New(
		service.Name("go.micro.service.user"),
		service.Version("latest"),
	)

	// service initial
	srv.Init()

	// create db connection
	db, err := gorm.Open("mysql", "root:123456@/micro?charset=utf8mb4&parseTime=True&loc=Local")
	if err != nil {
		panic(err)
	}
	defer db.Close()
	// 禁止复数名称
	db.SingularTable(true)

	// 创建表
	rp := repository.NewUserRepository(db)
	rp.InitTable()

	// Register handler
	userService := user_service.NewUserService(rp)
	pb.RegisterUserHandler(srv.Server(), &handler.User{UserService: userService})

	// Run service
	if err := srv.Run(); err != nil {
		logger.Fatal(err)
	}
}
